/**
 *  Copyright (C) 2008-2013  Telosys project org. ( http://www.telosys.org/ )
 *
 *  Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, Version 3.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *          http://www.gnu.org/licenses/lgpl.html
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.telosys.persistence.mapping;


/**
 * Mapping definition for an entity attribute
 * 
 * @author Laurent GUERIN
 *
 */
public class AttributeMapping 
{
	private final String   attributeName      ;
	
	private final String   dbColumnName         ;
	
	private final boolean  primaryKey          ;
	
	private final boolean  autoIncrementedColumn  ;

	private final int      keyPart  ;
	
	private final Object   complement  ;
	
	/**
	 * Constructor 
	 * @param attributeName
	 * @param dbColumnName
	 * @param primaryKey
	 * @param autoIncremented
	 * @param keyPart 
	 */
	public AttributeMapping(String attributeName, String dbColumnName, boolean primaryKey, boolean autoIncremented, int keyPart )
	{
		super();
		this.attributeName         = attributeName;
		this.dbColumnName          = dbColumnName;
		this.primaryKey            = primaryKey;
		this.autoIncrementedColumn = autoIncremented ;
		this.keyPart               = keyPart ;
		this.complement            = null ; 
	}

	/**
	 * Constructor with complementary information object 
	 * @param attributeName
	 * @param dbColumnName
	 * @param primaryKey
	 * @param autoIncremented
	 * @param keyPart
	 * @param complement
	 */
	public AttributeMapping(String attributeName, String dbColumnName, boolean primaryKey, boolean autoIncremented, int keyPart, Object complement )
	{
		super();
		this.attributeName         = attributeName;
		this.dbColumnName          = dbColumnName;
		this.primaryKey            = primaryKey;
		this.autoIncrementedColumn = autoIncremented ;
		this.keyPart               = keyPart ;
		this.complement            = complement ;
	}

	/**
	 * Returns the database column name
	 * @return
	 */
	public String getColumnName()
	{
		return dbColumnName ;
	}
	
	/**
	 * Returns the Java attribute name
	 * @return
	 */
	public String getAttributeName()
	{
		return attributeName ;
	}
	
	/**
	 * Return true if the attribute is the primary key (or a part of the primary key)
	 * @return
	 */
	public boolean isPrimaryKey()
	{
		return primaryKey ;
	}

	/**
	 * Return true if the attribute is an auto-incremented column in the database
	 * @return
	 */
	public boolean isAutoIncremented()
	{
		return autoIncrementedColumn ;
	}

	/**
	 * Returns the "Key Part" position for a composite key ( from 1 to N ) <br>
	 * or 0 if not defined
	 * @return
	 */
	public int getKeyPart() {
		return keyPart ;
	}
	
	/**
	 * Returns the instance of the object defining complementary information for the attribute (or null if none)
	 * @return
	 */
	public Object getComplement()
	{
		return complement ;
	}

	@Override
	public String toString() {
		return "AttributeMapping : '" 
				+ attributeName + "' --> '" + dbColumnName 
				+ "' (PK:"+ primaryKey + ","
				+ " auto-incr:" + autoIncrementedColumn + ")" 
				+ " " + ( complement != null ? complement : "" ) ;
	}
	
}
